package com.csd.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.csd.activity.CsdApplication;
import com.csd.activity.PaymentActivity;
import com.csd.activity.PersonalDetailActivity;
import com.csd.activity.PersonalOrderActivity;
import com.csd.activity.R;
import com.csd.bean.PersonalOrder;
import com.csd.fragment.PersonalAllFragment;

public class PersonalAllAdapter extends BaseAdapter {

	private List<PersonalOrder> list;
	private Context context;

	
	public PersonalAllAdapter(List<PersonalOrder> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		ViewHolder holder;
		LinearLayout.LayoutParams params = new 	LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(10, 10, 10, 10);
		PersonalOrder order = list.get(position);
		if (convertView == null) {
			view = LayoutInflater.from(context).inflate(R.layout.personalorder,
					null);
			holder = new ViewHolder();
			TextView tv_orderId = (TextView) view.findViewById(R.id.orderid);
			TextView tv_state = (TextView) view.findViewById(R.id.state);
			TextView tv_carbrand = (TextView) view.findViewById(R.id.carbrand);
			TextView tv_plate = (TextView) view.findViewById(R.id.plate);
			TextView tv_taketime = (TextView) view.findViewById(R.id.taketime);
			TextView tv_returntime = (TextView) view.findViewById(R.id.returntime);
			TextView tv_txt = (TextView) view.findViewById(R.id.pay_txt);
			TextView tv_payment = (TextView) view.findViewById(R.id.payment);
			ImageView img = (ImageView) view.findViewById(R.id.carimg);
			LinearLayout ll_detail = (LinearLayout) view.findViewById(R.id.detail);
			ll_detail.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(context, PersonalDetailActivity.class);
					context.startActivity(intent);
				}
			});
			LinearLayout ll_bt = (LinearLayout) view.findViewById(R.id.ll_bt);
			
			if (order.getState().equals("待付款")) {
				Button bt_pay = new Button(context);
				bt_pay.setText("付款");
				bt_pay.setLayoutParams(params);
				bt_pay.setBackgroundResource(R.drawable.circular_shape);
				bt_pay.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, PaymentActivity.class);
						context.startActivity(intent);
					}
				});
				Button bt_revise = new Button(context);
				bt_revise.setText("修改订单");
				bt_revise.setBackgroundResource(R.drawable.circular_shape);
				bt_revise.setLayoutParams(params);
				bt_revise.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					Toast.makeText(context, "修改订单", Toast.LENGTH_SHORT).show();	
					}
				});
				Button bt_cancle = new Button(context);
				bt_cancle.setText("取消订单");
				bt_cancle.setBackgroundResource(R.drawable.circular_shape);
				bt_cancle.setLayoutParams(params);
				bt_cancle.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					PersonalAllFragment.tv_cancle.setText("确定要取消订单");
					CsdApplication.showContent(PersonalAllFragment.ll_cancle, context);
					}
				});
				ll_bt.addView(bt_pay);
				ll_bt.addView(bt_revise);
				ll_bt.addView(bt_cancle);
			}
			if (order.getState().equals("已付款")) {
				Button bt_out = new Button(context);
				bt_out.setText("出车");
				bt_out.setBackgroundResource(R.drawable.circular_shape);
				bt_out.setLayoutParams(params);
				bt_out.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					Intent intent = new Intent(context, PersonalOrderActivity.class);
					intent.putExtra("num", 2);
					context.startActivity(intent);
					((Activity)context).finish();
					}
				});
				Button bt_cancel = new Button(context);
				
				bt_cancel.setText("取消订单");
				bt_cancel.setBackgroundResource(R.drawable.circular_shape);
				bt_cancel.setLayoutParams(params);
				bt_cancel.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						PersonalAllFragment.tv_cancle.setText("确定要取消订单");
						CsdApplication.showContent(PersonalAllFragment.ll_cancle, context);
					}
				});
				ll_bt.addView(bt_out);
				ll_bt.addView(bt_cancel);
			}
			holder.tv_orderId = tv_orderId;
			holder.tv_state = tv_state;
			holder.tv_carbrand = tv_carbrand;
			holder.tv_plate = tv_plate;
			holder.tv_taketime = tv_taketime;
			holder.tv_returntime = tv_returntime;
			holder.tv_paytxt = tv_txt;
			holder.tv_payment = tv_payment;
			holder.img = img;
			holder.ll_bt = ll_bt;
			holder.ll_detail = ll_detail;
			view.setTag(holder);
		}else{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}
		holder.tv_orderId.setText(order.getOrderId());
		holder.tv_state.setText(order.getState());
		holder.tv_carbrand.setText(order.getCar().getCarBrand());
		holder.tv_plate.setText(order.getCar().getPlate());
		holder.tv_taketime.setText(order.getTakeTime());
		holder.tv_returntime.setText(order.getReturnTime());
		if(order.getState().equals("待付款")){
			holder.tv_paytxt.setText("应付款：");
		}else if(order.getState().equals("已付款")){
			holder.tv_paytxt.setText("实付款：");
		}
		holder.tv_payment.setText("￥"+order.getPayment());
		holder.img.setImageResource(order.getCar().getImg());
		return view;
	}

	private class ViewHolder {
		TextView tv_orderId, tv_state, tv_carbrand, tv_plate, tv_taketime,
				tv_returntime, tv_paytxt, tv_payment;
		ImageView img;
		LinearLayout ll_bt,ll_detail;
	}
}
